<?php
/**
 +------------------------------------------------------------------------------
 * UserAction控制器类
 +------------------------------------------------------------------------------
 * @category   SubAction
 * @package  app
 * @subpackage  Action
 * @author   朝闻道 <hydata@gmail.com>
 * @date 2011-3-6
 * @time  下午04:53:55
 +------------------------------------------------------------------------------
 */
class UserAction extends Action {
	public function _initialize() {
		//$this->assign ( 'list', $list );
		header("Content-Type:text/html; charset=utf-8");
		//用户权限检查
		if ($_SESSION['admin']!="hezheng!#%") {
			$this->assign ( "jumpUrl", __APP__ . C ( "USER_AUTH_GATEWAY" ) );
			$this->error ( '你无权操作！' );
		}
	}
	//首页
	public function index() {
		$this->redirect ( 'User/cat' );
	}

	//设置权限的操作
	public function set() {
		if ($this->isPost ()) {
			$role = M ( 'Role' );
			$map = array ();
			$map ['id'] = $_POST ['id'];
			$value = array_sum ( $_POST ['name'] ); //将取得的数值进行或运算相当于求和操作，但是原理是不相同的哦
			$res = $role->where ( $map )->setField ( 'shell', $value );
			if ($res !== false) {
				$this->success ( '保存成功！' );
			} else {
				$this->error ( '保存失败' );
			}
		} else {
			$id = $_GET ['id'];
			$this->assign ( 'id', $id );
			$this->display ( 'User:cat' );
		}
	}
	

	
	/**
	 *处理编辑文章
	 *@date 2011-3-21
	 *@time 下午04:16:39
	 */
	function arc_do() {
		//处理编辑文章
		$id = $_POST ['id'];
		if (empty ( $_POST ['cid'] ) || empty ( $_POST ['title'] ) || empty ( $_POST ['content'] )) {
			$this->error ( "标题、分类、内容必须填写" );
		}
		$filename = '';
		if ($_FILES ['picture'] ['tmp_name']) {
			$filename = self::upf ( 'picture', 0 );
			//dump($filename);
			$filename = $filename ['url'];
		}
		$dao = D ( "Archives" );
		$vo = $dao->create ();
		if ($vo) {
			if ($filename) {
				$vo ['picture'] = $filename;
			} elseif ($_POST ['old_picture']) {
				$vo ['picture'] = $_POST ['old_picture'];
			}
			if ($id) {
				$dao->where ( "id='$id'" )->save ( $vo );
			} else {
				$id = $dao->add ( $vo );
			}
			if ($id) {
				$this->assign ( "jumpUrl", __APP__ . '/User/arc_add/cid/' . $vo ['cid'] . '.html' );
				$this->success ( "编辑成功" );
			} else { //dump($dao->getLastSql());
				$this->error ( "操作出错，请重试" );
			}
		} else {
			$this->error ( $dao->getDbError () );
		}
	} //end arc_do
	/**
	  	 *新加和编	 *@dat	 *@tim	 */
	function arc_add() { //新加和编辑文章
		$dao = D ( "Archives" );
		$act = $_GET ['act'];
		$id = $_GET ['id'];
		if ($act == 'edit' && isset ( $id )) {
			$dao = D ( "Archives" );
			$info = $dao->where ( "id='$id'" )->find ();
			$this->assign ( "info", $info );
		}
		$cid = intval ( $_GET ['cid'] );
		if($act == 'del'){
			$id = intval ( $_GET ['id'] );
			$num = $dao->where ( "id='$id'" )->limit ( '1' )->delete ();
			if ($num > 0) {
				$this->success ( "删除成功" );
			} else {
				$this->error ( "删除失败" );
			}
		}
		
		if ($cid) {
			$condition = array ();
			$condition ['cid'] = $cid;
			import ( '@.ORG.ZQPage' );
			$count = $dao->where ( $condition )->count ();
			$Page = new ZQPage ( $count, 10 );
			$show = $Page->show ();
			$this->assign ( 'showpage', $show );
			$p=$Page->getPage();
			$limit=$p==1||empty($p)?'':($p*10).',10';
			$arc_list = $dao->where ( $condition )->limit($limit)->findAll ();
			$this->assign ( "arc_list", $arc_list );
			//dump($dao->getLastSql());
		}

		import ( "ORG.Net.Keditor" );
		$ke = new Keditor ();
		$ke->id = "content"; //指定textarea的id
		$keshow = $ke->show (); //生成js代码
		$this->assign ( "keshow", $keshow );
		$dao = D ( "Cat" );
		$list = array ();
		$list = S ( "cat_list" );
		if (empty ( $list )) {
			$condition = array ();
			$list = $dao->where ( $condition )->findAll ();
			$list = list_to_tree ( $list, 'id', 'pid', '_son', '0' );
			$new_list = array ();
			foreach ( $list as $k => $v ) {
				$new_list [] = $v;
				if ($v ['_son']) {
					foreach ( $v ['_son'] as $sv ) {
						$sv ['type_name'] = '|--' . $sv ['type_name'];
						$new_list [] = $sv;
						if ($sv ['_son']) {
							foreach ( $sv ['_son'] as $ssv ) {
								$ssv ['type_name'] = '|--|--' . $ssv ['type_name'];
								$new_list [] = $ssv;
								if ($ssv ['_son']) {
									foreach ( $ssv ['_son'] as $sssv ) {
										$sssv ['type_name'] = '|--|--|--' . $sssv ['type_name'];
										$new_list [] = $sssv;
									}
								}
							}
						}
					}
				}
			}
			$list = $new_list;
			S ( "cat_list", $list, 900 );
		}
		$this->assign ( "cat_list", $list );
		$this->display ();
	} //end arc_add
	/**
	  	 *查看和处	 *@dat	 *@tim	 */
	function cat() { //查看和处理分类
		//load("extend");
		$dao = D ( "Cat" );
		$act = $_REQUEST ['act'];
		if (! empty ( $act )) {
			if (in_array ( $act, array ('add', 'edit', 'del', 'update' ) )) {
				switch (TRUE) {
					case $act == 'add' :
						$vo = $dao->create ();
						if ($vo) {
							$id = $dao->add ( $vo );
							if ($id) {
								S ( "cat_list", null );
								$this->success ( "添加成功" );
							} else {
								$this->error ( "添加失败" );
							}
						} else {
							$this->error ( $dao->getDbError () );
						}
						break;
					case $act == 'edit' :
						$id = intval ( $_GET ['id'] );
						$info = $dao->where ( "id='$id'" )->find ();
						$this->assign ( "info", $info );
						break;
					case $act == 'update' :
						$vo = $dao->create ();
						if ($vo) {
							$dao->save ( $vo );
							S ( "cat_list", null );
							$this->success ( "编辑成功" );
						} else {
							$this->error ( $dao->getDbError () );
						}
						break;
					case $act == 'del' :
						$id = intval ( $_GET ['id'] );
						$num = $dao->where ( "id='$id'" )->limit ( '1' )->delete ();
						if ($num > 0) {
							S ( "cat_list", null );
							$this->success ( "删除成功" );
						} else {
							$this->error ( "删除失败" );
						}
						break;
				}
			} else {
				$this->error ( "参数错误" );
			}
		}
		$list = array ();
		$list = S ( "cat_list" );
		if (empty ( $list )) {
			$condition = array ();
			$list = $dao->where ( $condition )->order ( "`order` ASC" )->findAll ();
			$list = list_to_tree ( $list, 'id', 'pid', '_son', '0' );
			$new_list = array ();
			foreach ( $list as $k => $v ) {
				$new_list [] = $v;
				if ($v ['_son']) {
					foreach ( $v ['_son'] as $sv ) {
						$sv ['type_name'] = '|--' . $sv ['type_name'];
						$new_list [] = $sv;
						if ($sv ['_son']) {
							foreach ( $sv ['_son'] as $ssv ) {
								$ssv ['type_name'] = '|--|--' . $ssv ['type_name'];
								$new_list [] = $ssv;
								if ($ssv ['_son']) {
									foreach ( $ssv ['_son'] as $sssv ) {
										$sssv ['type_name'] = '|--|--|--' . $sssv ['type_name'];
										$new_list [] = $sssv;
									}
								}
							}
						}
					}
				}
			}
			$list = $new_list;
			S ( "cat_list", $list, 900 );
		}
		$this->assign ( "list", $list );
		import ( "ORG.Net.Keditor" );
		$ke = new Keditor ();
		$ke->id = "about"; //指定textarea的id
		$keshow = $ke->show (); //生成js代码
		$this->assign ( "keshow", $keshow );
		$this->display ();
	} //end cat
	/**
	  	 *幻灯管理	 *@dat	 *@tim	 */
	function slide() { //幻灯管理
		;
	} //end Slide
	/**
	 *上传非图片文件
	 *@date 2011-3-26
	 *@time 下午02:03:49
	 */
	function upf($fname = 'picture', $is_json = '1') {
		//上传非图片文件
		$md5file = md5_file ( $_FILES [$fname] ['tmp_name'] );
		if ($md5file) {
			$dao = M ( "Pic" );
			$condition = array ("hash" => $md5file );
			$info = array ();
			$info = $dao->where ( $condition )->find ();
			if ($info) {
				$file_url = $info ['short'];
				//$imgid = __APP__ . '/dl/' . $info ['short'];
				$info ['return'] = array ('error' => 0, 'url' => $file_url );
			} else {
				import ( "ORG.Net.Keditor" );
				$ke = new Keditor ();
				$info = array ();
				$info = $ke->upload ( './Public/Upload/', '/Public/Upload/', array ('gif', 'jpg', 'jpeg', 'png', 'bmp', 'rar', 'zip', '7z' ), 3145728, $fname, 1 );
				$data = array ();
				$data ['path'] = $info ['file'] ['save_path'];
				$data ['path_url'] = $info ['file'] ['save_url'];
				$data ['images'] = $info ['file'] ['new_file_name'];
				$data ['hash'] = $md5file;
				$data ['ctime'] = time ();
				$data ['short'] = shorturl ( $data ['path_url'] . $data ['images'] );
				$id = $dao->add ( $data );
				if ($id) {
					$info ['return'] = array ('error' => 0, 'url' => $data ['short'] );
				}
			}
			//$info['return']['error']=$dao->getlastsql();
			if ($is_json == '1') {
				import ( "ORG.Net.JSON" );
				$json = new Services_JSON ();
				echo $json->encode ( $info ['return'] );
			} else {
				return $info ['return'];
			}
		}
	} //end upf
}//end UserAction